/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.srgsxmldb.servlets;

import cz.muni.fi.pb138.srgsxmldb.manager.IXManager;
import cz.muni.fi.pb138.srgsxmldb.manager.XManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * this class serves to search grammars by desired grammar root name
 * 
 * @author Eldred
 */
public class SearchServlet extends HttpServlet{

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.setContentType("text/html");

            IXManager manager = new XManager();
            String[] grams;
            String root = request.getParameter("root");
            if (root == null) {
                grams = manager.getAllGrammars();
            } 
            else {
                grams = manager.getGrammarFileNameByRoot(root);
            }

            request.setAttribute("grams", grams);
            request.setAttribute("root", root);
            request.setAttribute("requestedContent", "/grammars");

            try {
                request.getRequestDispatcher("/index").include(request, response);
            } catch (ServletException ex) {
                Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
    }
}

